#include "ImaJsonAssembler.h"
#include <jansson.h>
#include "ImaConstants.h"
#include "ImaLogger.h"

extern ImaLogger* gpLogger;

//{
//   "message": "request",
//   "source": "web-browser",
//   "__comment" : "below line: action == 1 means register",
//   "action": 1,
//   "details": {
//      "email": "knandangmail.com",
//      "password": "kamal",
//      "name": "Kamal",
//      "gender": "M",
//      "address": "1260, Sec 29, Noida",
//      "city": "Noida",
//      "state": "UP",
//      "country": "India",
//      "zip": "201301",
//      "phone": "+919971295647",
//      "__comment" : "date should be in the format yyyy-mm-dd",
//      "dob": "1981--22"
//   }
//}


char* ImaJsonAssembler::assembleUserFeedbackPkt(std::string& strSource,
					std::string& strSenderName,
					std::string& strSenderEmailId,
					std::string& strFeedbackSub,
					std::string& strFeedbackMsg) {
	LOG(debug, __FILE__, __LINE__, "ImaJsonAssembler::assembleUserFeedbackPkt");

	json_t* pRootObj = json_object();


	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION, json_integer(IMA_ACTION_USER_FEEDBACK));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_NAME, json_string(strSenderName.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL, json_string(strSenderEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SUBJECT, json_string(strFeedbackSub.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_BODY, json_string(strFeedbackMsg.c_str()));

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	char log_msg[1024];
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;

}

char* ImaJsonAssembler::assembleUserRegistrationPkt(string& source,
					string& emailId, string& password,
					string& name, string& gender, string& address,
					string& city, string& state, string& country,
					string& zip, string& phone, string& dob,
					string& imId, string& imService) {
	LOG(debug, __FILE__, __LINE__, "ImaJsonAssembler::assembleUserRegistrationPkt called");

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(source.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_REGISTER));

	json_t* pDetailsObj = json_object();
	if(emailId.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_EMAIL, json_string(emailId.c_str()));

	if(password.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_PASSWORD, json_string(password.c_str()));

	if(name.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_NAME, json_string(name.c_str()));

	if(gender.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_GENDER, json_string(gender.c_str()));

	if(address.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_ADDRESS, json_string(address.c_str()));

	if(city.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_CITY, json_string(city.c_str()));

	if(state.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_STATE, json_string(state.c_str()));

	if(country.length() > 0)	
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_COUNTRY, json_string(country.c_str()));

	if(zip.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_ZIP, json_string(zip.c_str()));

	if(phone.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_PHONE, json_string(phone.c_str()));

	if(dob.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_DOB, json_string(dob.c_str()));

	if(imId.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_IM_ID, json_string(imId.c_str()));

	if(imService.length() > 0)
		json_object_set_new(pDetailsObj, IMA_JSON_KEY_IM_SERVICE, json_string(imService.c_str()));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pDetailsObj);
	json_decref(pRootObj);

	char log_msg[1024];
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleRegistrationConfirmationPkt(string& source,
					string& emailId) {
	LOG(debug, __FILE__, __LINE__, "ImaJsonAssembler::assembleRegistrationConfirmationPkt");

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_CONFIRM_REGISTRATION));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(source.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(emailId.c_str()));

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	char log_msg[1024];
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleUpdateUserDetailsPkt(string& strSource,
					string& strEmailId, string& strImId, string& strName,
					string& strAddress, string& strCity,
					string& strState, string& strCountry, string& strZip, 
					string& strPhone, string& strGender, string& strDob) {
	char log_msg[1024];
	sprintf(log_msg, "assembleUpdateUserDetailsPkt called with emailid: %s", strEmailId.c_str());

	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_UPDATE_USER_DETAILS));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));

	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_IM_ID, json_string(strImId.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_NAME, json_string(strName.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_ADDRESS, json_string(strAddress.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_CITY, json_string(strCity.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_STATE, json_string(strState.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_COUNTRY, json_string(strCountry.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_ZIP, json_string(strZip.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_PHONE, json_string(strPhone.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_GENDER, json_string(strGender.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_DOB, json_string(strDob.c_str()));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleChangePasswordPkt(const std::string& source, 
					const std::string& strEmailId, const std::string& currPassword, 
					const std::string& newPassword) {
	LOG(debug, __FILE__, __LINE__, "ImaJsonAssembler::assembleChangePasswordPkt");

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_CHANGE_PASSWORD));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(source.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL, json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_PASSWORD, json_string(currPassword.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_NEW_PASSWORD,	json_string(newPassword.c_str()));

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	char log_msg[1024];
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleForgotPasswordPkt(const std::string& source, 
					const std::string& strEmailId) {
	LOG(debug, __FILE__, __LINE__, "ImaJsonAssembler::assembleForgotPasswordPkt");

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_FORGOT_PASSWORD));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(source.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL, json_string(strEmailId.c_str()));

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	char log_msg[1024];
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

//{
//        "message": "request",
//        "action": 3,
//        "source": "app",
//			"userid": "123"
//        "email": "kamal.nandan@gmail.com",
//        "password": "kamal"
//			"confcode": "123456"
//}

char* ImaJsonAssembler::assembleLoginPkt(std::string& source, std::string& emailId, std::string& password,
			const std::string& strConfCode) {
	char log_msg[1024];
	sprintf(log_msg, "assembleLoginPkt called with emailid: %s, password: %s", emailId.c_str(), password.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION, json_integer(IMA_ACTION_LOGIN));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(source.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL, json_string(emailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_PASSWORD, json_string(password.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_CONFCODE, json_string(strConfCode.c_str()));

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleLogoutPkt(string& source, string& emailId) {
	return NULL;
}

//{
//        "message": "request",
//        "action": 18,
//        "source": "app",
//        "email": "kamal.nandan@gmail.com",
//}
char* ImaJsonAssembler::assembleFetchUserDataPkt(std::string& source, std::string& emailId) {
	char log_msg[1024];
	sprintf(log_msg, "assembleFetchUserDataPkt called with emailid: %s", emailId.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_FETCH_USER_DATA));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(source.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(emailId.c_str()));

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleAddInfoPkt(const std::string& strSource, 
					const std::string& strEmailId, const std::string& strInfo,
					const int media, const std::string& strExpiryDate, 
					const unsigned int ttl) {
	char log_msg[1024];
	sprintf(log_msg, "assembleAddInfoPkt called with emailid: %s, info : %s", strEmailId.c_str(), strInfo.c_str());

	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_ADD_INFO));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));

	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_INFO, json_string(strInfo.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_MEDIA, json_integer(media));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_EXPIRY_DATE, json_string(strExpiryDate.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_TTL, json_integer(ttl));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}
		
char* ImaJsonAssembler::assembleUpdateInfoPkt(const std::string& strSource, const std::string& strEmailId, long& infoId, const std::string& strInfo, const int media, const std::string& strExpiryDate, const unsigned int ttl) {
	char log_msg[1024];
	sprintf(log_msg, "assembleUpdateInfoPkt called with infoId: %lu, emailid: %s, info : %s", infoId, strEmailId.c_str(), strInfo.c_str());

	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_UPDATE_INFO));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_INFO_ID, json_integer(infoId));

	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_INFO, json_string(strInfo.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_MEDIA, json_integer(media));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_EXPIRY_DATE, json_string(strExpiryDate.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_TTL, json_integer(ttl));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleUpdateInfoPkt(const std::string& strSource, const std::string& strEmailId, 
	long& infoId, const std::string& strInfo, const int media, const std::string& strExpiryDate, 
	const unsigned int ttl, std::vector<unsigned long>& recipientsToDisassociate) {
	char log_msg[1024];
	sprintf(log_msg, "assembleUpdateInfoPkt called with infoId: %lu, emailid: %s, info : %s", infoId, strEmailId.c_str(), strInfo.c_str());

	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_UPDATE_INFO));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_INFO_ID, json_integer(infoId));

	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_INFO, json_string(strInfo.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_MEDIA, json_integer(media));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_EXPIRY_DATE, json_string(strExpiryDate.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_TTL, json_integer(ttl));

	//The recipients that need to be disaasociated
	json_t* pRecipientsToDisassociateObj = json_array();
	std::vector<unsigned long>::iterator it1;
	for(it1 = recipientsToDisassociate.begin(); it1 != recipientsToDisassociate.end(); it1++) {
		unsigned long recipientId = *it1;
		json_array_append_new(pRecipientsToDisassociateObj, json_integer(recipientId));
	}
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_RECIPIENTSTODISASSOCIATE, pRecipientsToDisassociateObj);

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}


char* ImaJsonAssembler::assembleDeleteInfoPkt(const std::string& strSource, const std::string strEmailId, unsigned long infoId) {
	char log_msg[1024];
	sprintf(log_msg, "assembleDeleteInfoPkt called with emailid: %s, infoId : %lu", strEmailId.c_str(), infoId);

	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_DELETE_INFO));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_INFO_ID, json_integer(infoId));
	
	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

//{
//        "message": "request",
//        "action": 7,
//        "source": "app",
//        "email": "kamal.nandan@gmail.com",
//        "recipients":
//        {
//                "recipient":
//                {
//                        "info-id": 1234,
//                        "email": "paddy.roy.bt@gmail.com",
//                        "phone": "+919540738386",
//                        "address": "1260, sec 29, noida"
//                }
//        }
//}

char* ImaJsonAssembler::assembleAddInfoRecipientsPkt(
			const std::string& strSource, const std::string& strEmailId, 
			const unsigned long& infoId, 
			std::vector<INFO_RECIPIENT*>& recipientVector) {
	char log_msg[1024];
	sprintf(log_msg, "assembleAddInfoRecipientsPkt called with emailid: %s, infoid : %lu, no. of recipients: %d", strEmailId.c_str(), infoId, recipientVector.size());
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_ADD_INFO_RECIPIENTS));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_INFO_ID,	json_integer(infoId));

	json_t* pRecipientsObj = json_array();

	std::vector<INFO_RECIPIENT*>::iterator it;
	for(it = recipientVector.begin(); it != recipientVector.end(); it++) {
		LOG(debug, __FILE__, __LINE__, "Appending new object");
		json_t* pRecipientObj = json_object();
		INFO_RECIPIENT* pInfoRec = *it;

		if(pInfoRec->recipientId > 0) {
			json_object_set_new(pRecipientObj, IMA_JSON_KEY_ID, json_integer(pInfoRec->recipientId));
		} else {
			if(pInfoRec->strName.length())
				json_object_set_new(pRecipientObj, IMA_JSON_KEY_NAME, json_string(pInfoRec->strName.c_str()));
			if(pInfoRec->strEmailId.length())
				json_object_set_new(pRecipientObj, IMA_JSON_KEY_EMAIL, json_string(pInfoRec->strEmailId.c_str()));
			if(pInfoRec->strPhone.length())
				json_object_set_new(pRecipientObj, IMA_JSON_KEY_PHONE, json_string(pInfoRec->strPhone.c_str()));
			if(pInfoRec->strAddress.length())
				json_object_set_new(pRecipientObj, IMA_JSON_KEY_ADDRESS, json_string(pInfoRec->strAddress.c_str()));
			if(pInfoRec->strRelationship.length())
				json_object_set_new(pRecipientObj, IMA_JSON_KEY_RELATION_WITH_OWNER, json_string(pInfoRec->strRelationship.c_str()));
		}

		json_array_append_new(pRecipientsObj, pRecipientObj);

//		json_decref(pRecipientObj);
	}

	json_object_set_new(pRootObj, IMA_JSON_KEY_RECIPIENTS, pRecipientsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pRecipientsObj);

	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleDeleteInfoRecipientPkt(const std::string& strSource, const std::string& strEmailId, const unsigned long& infoId, const unsigned long& recipientId) {
	char log_msg[1024];
	sprintf(log_msg, "assembleDeleteInfoRecipientPkt called with emailid: %s, infoid : %lu, recipientid: %lu", strEmailId.c_str(), infoId, recipientId);
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_DELETE_INFO_RECIPIENT));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,	json_string(strEmailId.c_str()));

	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_INFO_ID,	json_integer(infoId));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_RECIPIENT_ID,	json_integer(recipientId));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);
	
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleAddRecipientPkt(const std::string& strSource, const std::string& strEmailId, const std::string& strRecipientName, const std::string& strRecipientEmailId, const std::string& strRecipientPhone, const std::string& strRecipientAddress, const std::string& strRecipientRelation) {
	char log_msg[1024];
	sprintf(log_msg, "ImaService::assembleUpdateRecipientPkt called with emailid: %s", strEmailId.c_str());
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_ADD_RECIPIENT));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,json_string(strEmailId.c_str()));
	
	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_NAME, json_string(strRecipientName.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_EMAIL, json_string(strRecipientEmailId.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_PHONE, json_string(strRecipientPhone.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_ADDRESS, json_string(strRecipientAddress.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_RELATION_WITH_OWNER, json_string(strRecipientRelation.c_str()));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);
	
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleUpdateRecipientPkt(const std::string& strSource, const std::string& strEmailId, const long& recipientId, const std::string& strRecipientName, const std::string& strRecipientEmailId, const std::string& strRecipientPhone, const std::string& strRecipientAddress, const std::string& strRecipientRelation) {
	char log_msg[1024];
	sprintf(log_msg, "ImaService::assembleUpdateRecipientPkt called with emailid: %s,  recipientid: %lu", strEmailId.c_str(), recipientId);
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_UPDATE_RECIPIENT));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_RECIPIENT_ID,	json_integer(recipientId));
	
	json_t* pDetailsObj = json_object();
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_NAME, json_string(strRecipientName.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_EMAIL, json_string(strRecipientEmailId.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_PHONE, json_string(strRecipientPhone.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_ADDRESS, json_string(strRecipientAddress.c_str()));
	json_object_set_new(pDetailsObj, IMA_JSON_KEY_RELATION_WITH_OWNER, json_string(strRecipientRelation.c_str()));

	json_object_set_new(pRootObj, IMA_JSON_KEY_DETAILS, pDetailsObj);

	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	json_decref(pDetailsObj);
	
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}

char* ImaJsonAssembler::assembleDeleteRecipientPkt(const std::string& strSource, const std::string& strEmailId, const long& recipientId) {
	char log_msg[1024];
	sprintf(log_msg, "ImaService::assembleDeleteRecipientPkt called with emailid: %s,  recipientid: %lu", strEmailId.c_str(), recipientId);
	LOG(debug, __FILE__, __LINE__, log_msg);

	json_t* pRootObj = json_object();
		
	json_object_set_new(pRootObj, IMA_JSON_KEY_MESSAGE, json_string(IMA_JSON_VALUE_REQUEST));
	json_object_set_new(pRootObj, IMA_JSON_KEY_ACTION,	json_integer(IMA_ACTION_DELETE_RECIPIENT));
	json_object_set_new(pRootObj, IMA_JSON_KEY_SOURCE, json_string(strSource.c_str())); 
	json_object_set_new(pRootObj, IMA_JSON_KEY_EMAIL,json_string(strEmailId.c_str()));
	json_object_set_new(pRootObj, IMA_JSON_KEY_RECIPIENT_ID,	json_integer(recipientId));
	
	char* pJsonString = json_dumps(pRootObj, JSON_INDENT(IMA_JSON_INDENT_SIZE));
	json_decref(pRootObj);
	
	sprintf(log_msg, "pJsonString is %s\n", pJsonString);
	LOG(debug, __FILE__, __LINE__, log_msg);

	return pJsonString;
}
